package com.biu.billiards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.biu.billiards.mapper.CouponMapper;
import com.biu.billiards.model.bo.BilliardsBO;
import com.biu.billiards.model.bo.GoodsBO;
import com.biu.billiards.model.entity.Billiards;
import com.biu.billiards.model.entity.Goods;
import com.biu.billiards.mapper.GoodsMapper;
import com.biu.billiards.model.vo.goods.AddGoodsVo;
import com.biu.billiards.model.vo.goods.GetGoodsVo;
import com.biu.billiards.model.vo.goods.UpdateGoodsVO;
import com.biu.billiards.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-09-25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {


    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Goods selectGoodsSku(String id) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id",id);

        System.out.println("ididid");
        System.out.println(id);

        return goodsMapper.selectOne(queryWrapper);
    }

    @Override
    public Goods selectGoods(Integer id) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",id);
        return goodsMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<GoodsBO> getGoodsPage(GetGoodsVo getGoodsVo) {

        System.out.println("getGoodsVo");
        System.out.println(getGoodsVo);
        QueryWrapper<Goods> query = new QueryWrapper<>();

        if (getGoodsVo.getShopId()!=null){
            query.like("shop_id",getGoodsVo.getShopId());
        }

        if (getGoodsVo.getType()!=null){
            query.like("type",getGoodsVo.getType());
        }

        if (getGoodsVo.getGoodsType()!=null){
            query.like("goods_type",getGoodsVo.getGoodsType());
        }

        //新建分页条件
        Page<Goods> p = new Page<>(getGoodsVo.getCurrentPage(),getGoodsVo.getPageSize());
        IPage<Goods> userIPage = goodsMapper.selectPage(p,query);
        IPage<GoodsBO> page = new Page<>();
        List<GoodsBO> adminDetailBOList = new ArrayList<>();
        for (Goods admin: userIPage.getRecords()){
            GoodsBO adminDetailBO = new GoodsBO();
            BeanUtils.copyProperties(admin,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());

        return page;
    }

    @Override
    public Boolean updateGoods(UpdateGoodsVO updateGoodsVO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",updateGoodsVO.getGoodsId());
        System.out.println("updateGoodsVO");
        System.out.println(updateGoodsVO);
        Goods billiards = goodsMapper.selectOne(queryWrapper);
        billiards.setGoodsPrice(updateGoodsVO.getGoodsPrice());
        billiards.setGoodsName(updateGoodsVO.getGoodsName());
        billiards.setPlayHour(updateGoodsVO.getPlayHour());
        billiards.setGoodsType(updateGoodsVO.getGoodsType());
        billiards.setOldPrice(updateGoodsVO.getOldPrice());
        billiards.setV0(updateGoodsVO.getV0());
        billiards.setV1(updateGoodsVO.getV1());
        billiards.setV2(updateGoodsVO.getV2());
        billiards.setShopId(updateGoodsVO.getShopId());




        billiards.setSkuId(updateGoodsVO.getSkuId());
        billiards.setStartTime(updateGoodsVO.getStartTime());
        billiards.setEndTime(updateGoodsVO.getEndTime());

        billiards.setType(updateGoodsVO.getType());

        return goodsMapper.update(billiards, queryWrapper) == 1;
    }

    @Override
    public Boolean addGoods(AddGoodsVo addGoodsVo) {

        System.out.println("addGoodsVo12313");
        System.out.println(addGoodsVo);
        Goods goods = new Goods();
        BeanUtils.copyProperties(addGoodsVo,goods);


        return goodsMapper.insert(goods) == 1;
    }

    @Override
    public Boolean deleteGoods(Integer goodsId) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId);
        return goodsMapper.delete(queryWrapper) == 1;
    }
}
